package sample.plugin.editor.activation.event;

import android.content.Context;
import android.content.res.AssetFileDescriptor;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.view.ViewGroup;
import android.view.animation.TranslateAnimation;

import com.qiplat.open.ui.util.VibratorUtils;
import com.rapid.api.component.widget.window.Window;
import com.rapid.api.component.widget.window.Windows;
import com.rapid.api.ext.framework.BaseObject;
import com.rapid.api.ext.plugin.AndroidPlugins;
import com.rapid.api.framework.domain.window.WindowEvent;
import com.rapid.framework.data.SharedPreferences;
import com.rapid.framework.vfs.VirtualFile;

import java.io.IOException;

import io.github.rosemoe.sora.event.ContentChangeEvent;
import io.github.rosemoe.sora.event.EventReceiver;
import io.github.rosemoe.sora.event.Unsubscribe;
import io.github.rosemoe.sora.widget.CodeEditor;
import sample.plugin.editor.activation.setting.ActivateSettingsBuilder;
import sample.plugin.editor.activation.view.FireworkView;

public class ActivateEditorEvent extends BaseObject implements WindowEvent {

    private boolean shade;
    private boolean fire;
    private boolean voice;
    private boolean lineVoice;
    private boolean vibrate;
    private MediaPlayer player;
    private MediaPlayer player2;

    private void checkInitialize() {
        if (player != null) {
            return;
        }
        SharedPreferences sharedPreferences = getSharedPreferences();
        this.shade = sharedPreferences.getBoolean(ActivateSettingsBuilder.KEY_SHADE, true);
        this.fire = sharedPreferences.getBoolean(ActivateSettingsBuilder.KEY_FIRE, true);
        this.voice = sharedPreferences.getBoolean(ActivateSettingsBuilder.KEY_VOICE, false);
        this.lineVoice = sharedPreferences.getBoolean(ActivateSettingsBuilder.KEY_LINE_VOICE, false);
        this.vibrate = sharedPreferences.getBoolean(ActivateSettingsBuilder.KEY_VIBRATE, false);
        Context context = AndroidPlugins.getContext(this);
        if (voice) {
            this.player = new MediaPlayer();
            try {
                String path = sharedPreferences.getString(ActivateSettingsBuilder.KEY_VOICE_FILE);
                if (path == null) {
                    AssetFileDescriptor descriptor = context.getAssets().openFd("voice.wav");
                    player.setDataSource(descriptor.getFileDescriptor(),
                            descriptor.getStartOffset(),
                            descriptor.getLength());
                } else {
                    player.setDataSource(path);
                }
                player.setAudioStreamType(AudioManager.STREAM_MUSIC);
                player.prepare();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if (lineVoice) {
            this.player2 = new MediaPlayer();
            try {
                String path = sharedPreferences.getString(ActivateSettingsBuilder.KEY_LINE_VOICE_FILE);
                if (path == null) {
                    AssetFileDescriptor descriptor = context.getAssets().openFd("line-voice.wav");
                    player2.setDataSource(descriptor.getFileDescriptor(),
                            descriptor.getStartOffset(),
                            descriptor.getLength());
                } else {
                    player2.setDataSource(path);
                }
                player2.setAudioStreamType(AudioManager.STREAM_MUSIC);
                player2.prepare();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public void onWindowCreate(Window window, VirtualFile virtualFile) {
        checkInitialize();
        if (!(window instanceof CodeEditor)) {
            return;
        }
        CodeEditor editor = (CodeEditor) window;
        editor.subscribeEvent(ContentChangeEvent.class, new EventReceiver<ContentChangeEvent>() {
            @Override
            public void onReceive(ContentChangeEvent event, Unsubscribe unsubscribe) {
                if (shade) {
                    TranslateAnimation animation = new TranslateAnimation(0, 10, 0, 10);
                    animation.setDuration(200);
                    editor.startAnimation(animation);
                    animation = new TranslateAnimation(10, 0, 10, 0);
                    animation.setDuration(200);
                    editor.startAnimation(animation);
                }
                if (voice && !event.getChangedText().toString().contains("\n")) {
                    player.start();
                }
                if (lineVoice
                        && event.getAction() == ContentChangeEvent.ACTION_INSERT
                        && event.getChangedText().toString().contains("\n")) {
                    player2.start();
                }
                if (vibrate) {
                    VibratorUtils.click(getActivity());
                }
            }
        });
        if (fire) {
            FireworkView view = new FireworkView(getActivity());
            view.bind(editor);
            ViewGroup decorView = (ViewGroup) getActivity().getWindow().getDecorView();
            decorView.addView(view);
        }
    }

    @Override
    public void onWindowAdd(Window window) {

    }

    @Override
    public void onWindowSelect(Window window) {

    }

    @Override
    public void onWindowRemove(Window window) {

    }

    @Override
    public void onSave(Windows windows, boolean b) {

    }
}
